﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MemoryPool<T> where T : new()
{
    Stack<T> _stack = new Stack<T>();
    int _count = 50;

     public MemoryPool(int count)
     {
         _count = count;
         _Alloc();
     }

    private void _Alloc()
    {
        for (int i = 0; i < _count; ++i)
            _stack.Push(new T());
    }
    
    public T Pop()
    {
        if (_stack.Count > 0)
        {
            return _stack.Pop();
        }
        else
        {
            _Alloc();
        }

        return _stack.Pop();        
    }

    public void Push( T data )
    {
        _stack.Push(data);
    }
}
